﻿using BJoin.Application.BObject;
using BJoin.Application.Entity;
using BJoin.Common;
using DapperExtensions;
using System;
using System.Collections.Generic;
using System.Text;

namespace BJoin.Application.IRepository
{
    public static class RepositoryHelper
    {
        public static IList<ISort> OrderByToSort<T>(string[] orderBy)
        {
            if (null == orderBy || orderBy.Length == 0)
            {
                if (typeof(T).PropertyExist(EntityConst.EntityUpdateTime))
                    return new List<ISort> { new Sort<T> { PropertyName = EntityConst.EntityUpdateTime, Ascending = false } };
                else
                    return new List<ISort> { new Sort<T> { PropertyName = EntityConst.EntityId, Ascending = false } };
            }
            else
                return Array.ConvertAll(orderBy, value => new Sort<T>
                {
                    PropertyName = value?.ToString().Replace("-", string.Empty),
                    Ascending = !((value?.ToString()).Contains("-"))
                });
        }

        public static void ObjectNotNull(object predicate)
        {
            if (null == predicate && ReflectionHelper.GetObjectValues(predicate, true).Count == 0) //all fields are null that is invalid
                throw new ArgumentException("delete with no predicate,forbidden operation");
        }
    }
}
